package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DishServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/3/13  15:35
 * @Version 1.0
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        //开始分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page  = dishMapper.pageQuery(dto);

        return  new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增菜品和对应口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);


        //一，向菜品表插入1条数据
        dishMapper.insert(dish);
        //获取id
        Long dishId = dish.getId();

//        向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){

            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表中插入数据
            dishFlavorMapper.insertBatch(flavors);

        }
}

    /**
     * 菜品批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否存在起售中
        for (Long id : ids) {
           Dish dish =  dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品起售中
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        }
        //当前菜品是否被套餐关联  setmeal_dish 关联两表的第三个表
        List<Long> setMealIds = setMealDishMapper.getSetMealIdsByDishIds(ids);
        if(setMealIds != null && setMealIds.size() > 0){
            //当前菜品被套餐关联

            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的菜品数据
    /*    for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品口味数据
            dishFlavorMapper.deleteByDishId(id);

        }*/
        //使用批量删除
        //delete from a where id in()
        //批量删除dish
        dishMapper.deleteByIds(ids);
        //批量删除flavor
        dishFlavorMapper.deleteByDishIds(ids);

    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //查询菜品数据
        Dish dish = dishMapper.getById(id);
//        查询口味数据
       List<DishFlavor>  dishFlavors =  dishFlavorMapper.getByDishId(id);
//        封装vo
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //修改菜品表
        dishMapper.update(dish);

        //修改口味表
        //删除口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() >0){
          flavors.forEach(dishFlavor -> {
              dishFlavor.setDishId(dishDTO.getId());
          });
          //插入n条数据
          dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish build = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return  dishMapper.list(build);
    }

    /**
     * 菜品的起售停售
     * @param status
     */
    @Override
    public void startOrStop(Integer status ,Long id) {
        //
        Dish status1 = Dish.builder().status(status).id(id).build();
        List<Long> list = new ArrayList<Long>();
        list.add(status1.getId());
        //停售菜品时停售相关套餐
        if(status == StatusConstant.DISABLE){
            List<Long> setmealIds = setMealDishMapper.getSetMealIdsByDishIds(list);
            setmealIds.forEach(set1 ->{
                Setmeal setmeal = new Setmeal();
                setmeal.setId(set1);
                setmeal.setStatus(StatusConstant.DISABLE);
                setmealMapper.update(setmeal);
            });
        }


        dishMapper.update(status1);

    }

    /**
     * 查询菜品及口味 vo类型
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> list = dishMapper.list(dish);
        List<DishVO> dishVOS = new ArrayList<>();

        if(list != null && list.size() >0){
            list.forEach(dish1 -> {
                DishVO dishVO = new DishVO();
                    BeanUtils.copyProperties(dish1,dishVO);
                    dishVO.setFlavors(dishFlavorMapper.getByDishId(dish1.getId()));
                    dishVOS.add(dishVO);
            });
        }
        return dishVOS;

    }


}
